import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/login-interface.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login-interface',
      component: ()=> import('../views/login-interface.vue')
    },
    {
      path:'/home-page',
      name:'home-page',
      component:()=>import('../views/home-page.vue')
    },
    {
      path:'/register',
      name:'register',
      component:()=>import('../views/register.vue')
    },
    {
      path:'/test',
      component:()=>import('../views/test.vue')
    },
    {
      path:'/seekingjob',
      component:()=>import('../views/seekingjob.vue')
    },
    {
      path:'/employ',
      component:()=>import('../views/employ.vue')
    },
    {
      path:'/recommendation',
      component:()=>import('../views/recommendation.vue')
    },
    {
      path:'/user-manage',
      component:()=>import('../views/user-manage.vue')
    },
    {
      path:'/record',
      component:()=>import('../views/record.vue')
    },
    {
      path:'/userlimit',
      component:()=>import('../views/userlimit.vue')
    },
    {
     path:'/resumebox',
      component:()=>import('../views/resumebox.vue')
    },
    {
      path:'/Inbox',
      name:'Inbox',
      component:()=>import('../views/Inbox.vue')
    },
    {
      path:'/EmpRecord',
      name:'EmpRecord',
      component:()=>import('../views/EmpRecord.vue')
    },
    {
      path:'/community',
      name:'community',
      component:()=>import('../views/community.vue'),
    },
    {
      path:'/PersonalHomepage',
      name:'PersonalHomepage',
      component:()=>import('../views/PersonalHomepage.vue')
    },
    {
      path:'/demo',
      component:()=>import('../views/demo.vue')
    },
    {
      path:'/403',
      name:'auth',
      component:()=>import('../views/auth.vue')
    },

  ]
})

//路由守卫
router.beforeEach((to,from,next) =>{
  if (to.path === '/' || to.path === '/register'){
    next();
  }
  const user = localStorage.getItem("user");
  if(!user && to.path !== '/'){
        return next("/");
  }
  next();
})

//from为开源的路由信息 next()为跳转路由的函数
router.beforeEach((to, from, next)=>{
  let adminpath1=['/user-manage','/userlimit'];
  let user=JSON.parse(localStorage.getItem("user") || '{}');

  if(user.role !== 'admin' && adminpath1.includes(to.path)){
    next('/403')
  }else {
    next()
  }
})
export default router
